package com.canbot.u05.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;

import com.canbot.u05.R;

public class PresideSpeedView extends View {

        private static final String TAG = "CircleProgressView";

        private int measuredWidth;

        private int measuredHeight;

        private Path mPath;

        private Paint mPaint;

        private int mMaxR;

        private float mInR;

        private RectF mRectF;

        private Paint mBluePaint;

        private Paint mGreenPaint;

        private GestureDetector mGestureDetector;

        private Bitmap mBitMap;

        public int getIndex() {
                return mIndex;
        }

        private int mIndex;

        private Matrix mMatrix;

        public PresideSpeedView(Context context) {
                this(context, null);
        }

        public PresideSpeedView(Context context, @Nullable AttributeSet attrs) {
                this(context, attrs, 0);
        }

        public PresideSpeedView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
                super(context, attrs, defStyleAttr);
                init();
        }

        private void init() {
                mIndex = 1;
                mBitMap = BitmapFactory.decodeResource(getResources(), R.drawable.preside_speed2);
                mGestureDetector = new GestureDetector(getContext(), onGestureListener);
                mMatrix = new Matrix();
        }

        private void setIndex(int index) {
                if (this.mIndex == index) {
                        return;
                }
                this.mIndex = index;
                if (mIChooseListener != null) {
                        mIChooseListener.choose(index);
                }
                mBitMap.recycle();
                switch (index) {
                        case 0:
                                mBitMap = BitmapFactory.decodeResource(getResources(), R.drawable.preside_speed1);
                                break;
                        case 1:
                                mBitMap = BitmapFactory.decodeResource(getResources(), R.drawable.preside_speed2);
                                break;
                        case 2:
                                mBitMap = BitmapFactory.decodeResource(getResources(), R.drawable.preside_speed3);
                                break;
                        default:
                                mBitMap = BitmapFactory.decodeResource(getResources(), R.drawable.preside_speed1);
                                break;
                }
                invalidate();
        }

        public void setSpeed(int index) {
                if (this.mIndex == index) {
                        return;
                }
                if (index < 0) {
                        return;
                }
                else if (index > 2) {
                        return;
                }
                setIndex(index);
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
                super.onSizeChanged(w, h, oldw, oldh);
                measuredWidth = getMeasuredWidth();
                measuredHeight = getMeasuredHeight();
                setIndex(mIndex);
        }

        @Override
        protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                mMatrix.reset();
                if (mBitMap == null || mBitMap.isRecycled()) {
                        return;
                }
                float scale = Math.min(measuredWidth * 1.0F / mBitMap.getWidth(), measuredHeight * 1.0F / mBitMap.getHeight());
                mMatrix.setScale(scale, scale);
                mMatrix.postTranslate(measuredWidth / 2 - mBitMap.getWidth() * scale / 2, measuredHeight / 2 - mBitMap.getHeight() * scale / 2);
                canvas.drawBitmap(mBitMap, mMatrix, null);
        }

        private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.OnGestureListener() {

                @Override
                public boolean onDown(MotionEvent e) {
                        float x = e.getX();
                        float y = e.getY();
                        check(x, y);
                        return false;
                }

                @Override
                public void onShowPress(MotionEvent e) {

                }

                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                        return false;
                }

                @Override
                public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                        float x = e2.getX();
                        float y = e2.getY();
                        check(x, y);
                        return false;
                }

                @Override
                public void onLongPress(MotionEvent e) {

                }

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                        return false;
                }
        };

        private void check(float x, float y) {
                double angle = Math.atan2(y - measuredHeight, x - measuredWidth / 2);
                int ceil = (int) Math.ceil(-angle * 3 / Math.PI);
                setIndex(3 - ceil);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_DOWN) {
                        performClick();
                }
                mGestureDetector.onTouchEvent(event);
                return true;
        }

        @Override
        public boolean performClick() {
                return super.performClick();
        }

        public void setIChooseListener(IChooseListener mIChooseListener) {
                this.mIChooseListener = mIChooseListener;
        }

        private IChooseListener mIChooseListener;

        public interface IChooseListener {

                void choose(int index);
        }
}
